# coding:utf-8
"""
description: motion sensor，Smoke sensor　Status弹框和smart button setting page
"""
import os
import random

from appium.webdriver.common import mobileby

from common.Gesture_Mainpulation import gesture_mainpulation
from common.Get_Yaml import get_yaml
from common.Log import LOG, logger
from config.GlobalParameter import TEST_Element_YAML
from pages.Scenes_page import ScenesList_page

Datas = get_yaml(os.path.join(TEST_Element_YAML, 'Devices', 'SensorsButtons.yaml'))


class SensorsButtons_page(ScenesList_page):
    by = mobileby.MobileBy()

    # Smoke sensor-----------------------------Device Setting》点击右上角？ icon
    btnDeviceSettingsTip_loc = (by.ID, Datas.get_elementInfo(0))

    def click_btnDeviceSettingsTip(self):
        """Smoke sensor》Device Setting》点击右上角？ icon"""
        self.find_element(*self.btnDeviceSettingsTip_loc).click()

    # Smoke sensor》Device Setting》获取弹框内所有文字
    SmokeTextView_loc = (by.CLASS_NAME, Datas.get_elementInfo(1))

    def get_SmokeTextView(self):
        """Smoke sensor》Device Setting》获取弹框内所有文字"""
        T = []
        for index, row in enumerate(self.find_elements(*self.SmokeTextView_loc)):
            T.append(row.text)
        LOG.info(T[0])  # 打印第一个弹框标题
        for i in range(1, len(T), 2):
            b = T[i:i + 2]  # 分两列打印
            LOG.info(b)

    # Smoke sensor》Device Setting》点击ok按钮
    btnPopupSmokeTipOk_loc = (by.ID, Datas.get_elementInfo(2))

    def click_btnPopupSmokeTipOk(self):
        """Smoke sensor》Device Setting》点击ok按钮"""
        self.find_element(*self.btnPopupSmokeTipOk_loc).click()

    def func_GetSmokeText(self):
        """
        获取Smoke tips的内容
        :return:
        """
        self.click_btnDeviceSettingsTip()
        self.get_SmokeTextView()
        self.click_btnPopupSmokeTipOk()

    # Motion sensor-----------------------------Device Setting》点击hold time，弹框内容见Public.yaml（7，8，9）
    MotionHoldTime_loc = (by.ID, Datas.get_elementInfo(3))

    def click_MotionHoldTime(self):
        """Motion sensor》Device Setting》点击hold time，弹框内容见Public.yaml（7，8，9）"""
        self.find_element(*self.MotionHoldTime_loc).click()

    def getText_MotionHoldTime(self):
        """Motion sensor》Device Setting》点击hold time，弹框内容见Public.yaml（7，8，9）"""
        return self.find_element(*self.MotionHoldTime_loc).text

    def func_MotionHoldTime(self, indexs=0, upAndDown="up", times=1):
        """
        操作Motion Hold Time
        :return:
        """
        gesture_mainpulation().swipe_up(self.driver, self.get_DeviceSettingsPage())  # 滑动Setting page
        ago = self.getText_MotionHoldTime()
        self.click_MotionHoldTime()
        super().func_NumberPickerComfirm(indexs, upAndDown, times)
        return ago != self.getText_MotionHoldTime()

    # smart Button-----------------------------Device Setting》点击button color一行
    CustomButtonLine_loc = (by.ID, Datas.get_elementInfo(4))

    def get_CustomButtonLine(self):
        """smart Button》Device Setting》点击button color一行"""
        return self.find_element(*self.CustomButtonLine_loc)

    # smart Button》Device Setting》选择red
    CustomButtonPopupRed_loc = (by.ID, Datas.get_elementInfo(5))

    def click_CustomButtonPopupRed(self):
        """smart Button》Device Setting》选择red"""
        self.find_element(*self.CustomButtonPopupRed_loc).click()

    # smart Button》Device Setting》选择黑色
    ButtonPopupCharcoal_loc = (by.ID, Datas.get_elementInfo(6))

    def click_ButtonPopupCharcoal(self):
        """smart Button》Device Setting》选择黑色"""
        self.find_element(*self.ButtonPopupCharcoal_loc).click()

    # smart Button》Device Setting》选择白色
    ButtonPopupWhite_loc = (by.ID, Datas.get_elementInfo(7))

    def click_ButtonPopupWhite(self):
        """smart Button》Device Setting》选择白色"""
        self.find_element(*self.ButtonPopupWhite_loc).click()

    # smart Button》Device Setting》点击ok按钮，弹框Close popup见Public.yaml
    ButtonPopupSave_loc = (by.ID, Datas.get_elementInfo(8))

    def click_ButtonPopupSave(self):
        """smart Button》Device Setting》点击ok按钮，弹框Close popup见Public.yaml"""
        self.find_element(*self.ButtonPopupSave_loc).click()

    # smart Button》Device Setting》获取三色文字，Red|Charcoal|White
    CustomButtonPopup_loc = (by.ID, Datas.get_elementInfo(8))

    def gettext_CustomButtonPopup(self):
        """smart Button》Device Setting》获取三色文字，Red|Charcoal|White"""
        return self.find_element(*self.CustomButtonPopup_loc).text

    def func_EditColour(self):
        """
        随机选择Colour
        :return:
        """
        self.get_CustomButtonLine().click()
        rr = random.randint(1, 3)  # 随机选择Button there Colour
        if rr == 1:
            self.click_CustomButtonPopupRed()
        elif rr == 2:
            self.click_ButtonPopupCharcoal()
        else:
            self.click_ButtonPopupWhite()
        Colour = self.gettext_CustomButtonPopup()  # 获取随机点击后的Colour文字
        self.click_ButtonPopupSave()
        return Colour == self.get_CustomButtonLine()

    # smart Button | Smart station》Device Setting》Press once + icon
    ButtonTask1_loc = (by.ID, Datas.get_elementInfo(9))

    def click_ButtonTask1(self):
        """smart Button | Smart station》Device Setting》Press once + icon"""
        self.find_element(*self.ButtonTask1_loc).click()

    # smart Button | Smart station》Device Setting》Double -click + icon
    ButtonTask2_loc = (by.ID, Datas.get_elementInfo(10))

    def click_ButtonTask2(self):
        """smart Button | Smart station》Device Setting》Double -click + icon"""
        self.find_element(*self.ButtonTask2_loc).click()

    # smart Button | Smart station》Device Setting》点击+ icon后的弹框Device一行
    taskpicker_device_loc = (by.ID, Datas.get_elementInfo(11))

    def click_taskpicker_device(self):
        """smart Button | Smart station》Device Setting》点击+ icon后的弹框Device一行"""
        self.find_element(*self.taskpicker_device_loc).click()

    # smart Button | Smart station》Device Setting》点击+ icon后的弹框Scene一行
    taskpicker_scene_loc = (by.ID, Datas.get_elementInfo(12))

    def click_taskpicker_scene(self):
        """smart Button | Smart station》Device Setting》点击+ icon后的弹框Scene一行"""
        self.find_element(*self.taskpicker_scene_loc).click()

    # smart Button | Smart station》Device Setting》点击+ icon后的弹框Cancel\已添加任务后变为Remove
    taskpicker_cancel_loc = (by.ID, Datas.get_elementInfo(13))

    def get_taskpicker_cancel(self):
        """smart Button | Smart station》Device Setting》点击+ icon后的弹框Cancel|
        已添加任务后变为Remove.但点击Cancel是没有任何反应的"""
        return self.find_element(*self.taskpicker_cancel_loc)

    # smart Button | Smart station》Device Setting》单选框，text为Off\On
    switchButtonTask1_loc = (by.ID, Datas.get_elementInfo(14))

    def click_switchButtonTask1(self):
        """smart Button | Smart station》Device Setting》单选框，text为Off|On"""
        self.find_element(*self.switchButtonTask1_loc).click()

    # smart Button | Smart station》Device Setting》获取或者点击task 1 name
    ButtonTask1Name_loc = (by.ID, Datas.get_elementInfo(15))

    def click_ButtonTask1Name(self):
        """smart Button | Smart station》Device Setting》获取或者点击task 1 name"""
        self.find_element(*self.ButtonTask1Name_loc).click()

    # smart Button | Smart station》Device Setting》Double click单选框，text为Off\On
    switchButtonTask2_loc = (by.ID, Datas.get_elementInfo(16))

    def click_switchButtonTask2(self):
        """smart Button | Smart station》Device Setting》Double click单选框，text为Off|On"""
        self.find_element(*self.switchButtonTask2_loc).click()

    # smart Button | Smart station》Device Setting》获取或者点击task 2 name
    ButtonTask2Name_loc = (by.ID, Datas.get_elementInfo(17))

    def click_ButtonTask2Name(self):
        """smart Button | Smart station》Device Setting》获取或者点击task 2 name"""
        self.find_element(*self.ButtonTask2Name_loc).click()

    # smart Button》Device Setting》Press Hold +icon
    ButtonTask3_loc = (by.ID, Datas.get_elementInfo(18))

    def click_ButtonTask3(self):
        """smart Button》Device Setting》Press Hold +icon"""
        self.find_element(*self.ButtonTask3_loc).click()

    # smart Button》Device Setting》点击+ icon后的弹框Emergency一行
    taskpicker_emergency_loc = (by.ID, Datas.get_elementInfo(19))

    def click_taskpicker_emergency(self):
        """smart Button》Device Setting》点击+ icon后的弹框Emergency一行"""
        self.find_element(*self.taskpicker_emergency_loc).click()

    # smart Button》Device Setting》Press Hold 单选框，text为Off\On
    switchButtonTask3_loc = (by.ID, Datas.get_elementInfo(20))

    def click_switchButtonTask3(self):
        """smart Button》Device Setting》Press Hold 单选框，text为Off|On"""
        self.find_element(*self.switchButtonTask3_loc).click()
        LOG.info("修改Button|Smart station的task，点击switch，Off|On》》》")

    # smart Button》Device Setting》获取或者点击task 3 name
    ButtonTask3Name_loc = (by.ID, Datas.get_elementInfo(21))

    def click_ButtonTask3Name(self):
        """smart Button》Device Setting》获取或者点击task 3 name"""
        self.find_element(*self.ButtonTask3Name_loc).click()

    def func_addTask(self, message):
        """
        添加Button 或者Smart station task任务，将会进行多次添加操作
        :param message: 断言内容
        :return:
        """
        gesture_mainpulation().swipe_up(self.driver, self.get_DeviceSettingsPage())  # 滑动Setting page
        if super().assertVisible(*self.ButtonTask1_loc):
            self.click_ButtonTask1()
            self.get_taskpicker_cancel().click()  # 进行Cancel操作
            self.click_ButtonTask1()  # 重新再点击才行
            return self.func_SelectTaskWay(message)
        elif super().assertVisible(*self.ButtonTask2_loc):
            self.click_ButtonTask2()
            return self.func_SelectTaskWay(message)
        elif super().assertVisible(*self.ButtonTask3_loc):
            # station没有Task 3，不会用到此方法
            self.click_ButtonTask3()
            return self.func_SelectTaskWay(message)
        else:
            LOG.info("没有找到 “+” icon入口进行新添加task，将进行Edit Task操作测试》》》")
            return True

    def func_EditTask(self, message):
        """
        修改Button 或者Smart station的task
        :param message: 弹框内容
        :return:
        """
        if super().assertVisible(*self.ButtonTask1Name_loc):
            if random.randint(1, 2) == 1:  # 随机操作switch还是edit task
                self.click_switchButtonTask1()
                return super().find_toast(message)
            else:
                self.click_ButtonTask1Name()
                return self.func_SelectTaskWay(message)
        elif super().assertVisible(*self.ButtonTask2Name_loc):
            self.click_ButtonTask2Name()
            return self.func_SelectTaskWay(message)
        elif super().assertVisible(*self.ButtonTask3Name_loc):
            # station没有Task 3，不会用到此方法
            self.click_switchButtonTask3()
            return super().find_toast(message)

    def func_SelectTaskWay(self, message):
        """
        随机点击选择Device、scene、（适用Button》Emergency）和Remove选项，进行断言（不做 Cancel按钮操作）
        :param message:弹框内容
        :return:
        """
        rr = random.randint(1, 1)  # 随机选择点击添加device、scene
        if rr == 1:
            self.click_taskpicker_device()
            LOG.info("添加或修改Button|Smart station的task，点击选择Device》》》")
            # 判断Overwrite Button task弹框是否存在，存在点击Overwrite
            self.assertVisibleAndClick_confirm()
            super().click_TaskDevices()  # 选择Task
            super().func_taskDevice()  # 根据Device 详情页做出操作确认
            return super().find_toast(message)
        elif rr == 2:
            self.click_taskpicker_scene()
            LOG.info("添加或修改Button|Smart station的task，点击选择scene》》》")
            # 判断Overwrite Button task弹框是否存在，存在点击Overwrite
            self.assertVisibleAndClick_confirm()
            random.choice(super().getAdded_SceneName()).click()
            super().click_confirm()  # Save Button Action 弹框的Comfirm
            return super().find_toast(message)
        elif rr == 3:
            # 这里判断是区分Station 和smart Station task。Station没有Emergency
            if super().assertVisible(*self.taskpicker_emergency_loc):
                self.click_taskpicker_emergency()
                LOG.info("添加或修改Button的task，点击选择emergency》》》")
                # 判断Overwrite Button task弹框是否存在，存在点击Overwrite
                self.assertVisibleAndClick_confirm()
                super().click_EditSceneSave()
                return super().find_toast(message)
            else:
                LOG.info("随机数{}，进入添加Emergency方法。station没有Emergency，直接断言为True》》》".format(rr))
                self.get_taskpicker_cancel().click()  # 关闭弹框
                return True
        else:
            if self.get_taskpicker_cancel().text == "Remove":  # 只做Remove操作
                LOG.info("添加或修改Button|Station 的task，点击选择Remove》》》")
                self.get_taskpicker_cancel().click()  # 点击Remove
                # 判断Remove Button task弹框是否存在，存在点击remove
                self.assertVisibleAndClick_confirm()
                return super().find_toast(message)
